﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace DotNET4Study
{
    public static class ThreadJoinStudy
    {
        /*
         * 当NewThread调用Join方法的时候，MainThread就被停止执行，直到NewThread线程执行完毕。
         * 可以尝试着将newThread.Join()注释掉，其显示结果就会不同。
         */
        public static void Excute()
        {
            Thread.CurrentThread.Name = "MainThread";

            Thread newThread = new Thread(ThreadFuncOne);
            newThread.Name = "NewThread";

            for (int j = 0; j < 20; j++)
            {
                if (j == 10)
                {
                    newThread.Start();
                    newThread.Join();
                }
                else
                {
                    Console.WriteLine(Thread.CurrentThread.Name + "   j =  " + j);
                }
            }
        }

        private static void ThreadFuncOne()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(Thread.CurrentThread.Name + "   i =  " + i);
                Thread.Sleep(50);
            }
            Console.WriteLine(Thread.CurrentThread.Name + " has finished");
        }
    }
}
